<template>
    <el-card>
        <div slot="header">
            <span>修改密码</span>
        </div>

        <el-form ref="form" :model="form" label-width="100px" status-icon :rules="rules">
            <el-form-item label="原密码" prop="oldPwd">
                <el-input v-model="form.oldPwd" type="password"></el-input>
            </el-form-item>

         

            <el-form-item label="新密码" prop="password">
                <el-input v-model="form.password" type="password"></el-input>
            </el-form-item>

            <el-form-item label="确认新密码" prop="newPwd">
                <el-input v-model="form.newPwd" type="password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">确认</el-button>
                <el-button>重置</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
import { changePad ,oldPad} from '@/api/user.js'
export default {
    data() {
        // 自定义验证规则函数
        let confirmPwd = (rule, value, callback) => {
            if (value != this.form.password) {
                callback(new Error('两次输入的密码不一致'));
            } else {
                callback();
            }
        }

        return {
            form: {
                oldPwd: '',//旧密码
                newPwd: '',//新密码
                password: '',
            },
            //验证规则对象
            rules: {
                oldPwd: [
                    { required: true, message: '请输入原始密码', trigger: 'blur' },
                    { min: 6, message: '请输入6位数以上的密码', trigger: 'blur' },
                ],
                password: [
                { required: true, message: '请再次输入新密码', trigger: 'blur' },
                { min: 6, message: '请输入6位数以上的密码', trigger: 'blur' },
                ],
                newPwd: [
                { required: true, message: '请输入新密码', trigger: 'blur' },
                    { validator: confirmPwd, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        onSubmit() {
            //提交时统一验证
            this.$refs.form.validate(async result => {
                if (result) {
                    console.log(this.form.oldPwd);
                    let old = await oldPad({
                        oldPwd:this.form.oldPwd
                    })
                    console.log(old);
                    if(old.data.code === 200){
                        let res = await changePad({newPwd:this.form.newPwd})
                        console.log('res',res);
                        }
                    

                } else {
                    console.log('验证未通过');
                }
            })
        }
    }
}
</script>

<style lang="less" scoped>
.el-form {
    width: 400px;
}
</style>